import util from '@/libs/util';
import Cookies from 'js-cookie';
import CONSTANTS from '@/config/constants';

export default {
    name: 'exportable-table',
    props: ['themeId'],
    data () {
        return {
            columns: [{
                type: 'selection',
                width: 60,
                align: 'center'
            }, {
                title: '用户名称',
                key: 'realname',
                render: (h, params) => {
                    return h('div', {}, params.row.member.realname);
                }
            }, {
                title: '联系电话',
                key: 'phone',
                render: (h, params) => {
                    return h('div', {}, params.row.member.phone);
                }
            }, {
                title: '性别',
                key: 'sex',
                render: (h, params) => {
                    return h('div', {}, (params.row.member.sex === 1 ? '男' : '女'));
                }
            }, {
                title: '年龄',
                key: 'age',
                render: (h, params) => {
                    return h('div', {}, params.row.member.age);
                }
            }, {
                title: '学校/所在单位',
                key: 'school',
                render: (h, params) => {
                    return h('div', {}, params.row.member.school);
                }
            }, {
                title: '备用1',
                key: 'spare1'
            }, {
                title: '备用2',
                key: 'spare2'
            }, {
                title: '备用3',
                key: 'spare3'
            }, {
                title: '奖项',
                key: 'diplomas',
                render: (h, params) => {
                    return h('div', {}, params.row.diplomas.name);
                }
            }, {
                title: '团员',
                key: 'team',
                render: (h, params) => {
                    return h('div', {}, params.row.team);
                }
            }, {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            query: {
                realname: '',
                phone: ''
            },

            total: 0,
            pageIndex: 1,
            pageSize: 100,
            SERVER_HOST: CONSTANTS.SERVER_HOST,
            downloadUrl: CONSTANTS.LOCAL_HOST + '/static/file/memberImport.xlsx',
            theme: '',
            ids: [],
            editModal: false,
            formData: {},
            dipList: [],
            ruleInline: {
                realname: [
                    { required: true, message: '会员名称不能为空！', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '联系电话不能为空！', trigger: 'blur' }
                ],
                school: [
                    { required: true, message: '学校/所在单位不能为空！', trigger: 'blur' }
                ],
                diplomasId: [
                    { type: 'number', required: true, message: '奖项不能为空！', trigger: 'change' }
                ]
            }
        };
    },
    watch: {
        themeId: function (newVal, oldVal) {
            this.theme = newVal;
            this.loadList();
        }
    },
    methods: {
        loadList () {
            if (this.theme) {
                util.ajax.post('/busine/theme/member/getMemberByTheme', util.toFormData({
                    page: this.pageIndex,
                    size: this.pageSize,
                    orgid: this.baseuser.orgId,
                    themeId: this.theme,
                    realName: this.query.realname,
                    phone: this.query.phone
                })).then(res => {
                    this.data = res.data.data.list;
                    this.total = res.data.data.total;
                });
            }
        },
        loadDip () {
            if (this.theme) {
                util.ajax.post('/busine/diplomas/findByTheme', util.toFormData({
                    themeId: this.theme
                })).then(res => {
                    this.dipList = res.data.data;
                });
            }
        },
        refresh () {
            this.query = {
                realname: '',
                phone: ''
            };
            this.loadList();
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/theme/member/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        uploadSuccess (res, file) {
            if (res.message === 'SUCCESS') {
                this.$Modal.success({
                    content: res.data,
                    onOk: () => {
                        this.loadList();
                    }
                });
            } else {
                this.$Modal.error({
                    content: res.message
                });
            }
        },
        getSelect (ref) {
            this.ids = [];
            ref.forEach((item) => {
                this.ids.push(item.id);
            });
        },
        deleteByIds () {
            if (this.ids.length > 0) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '<p>确定删除所选用户？</p>',
                    onOk: () => {
                        util.ajax.post('/busine/theme/member/deleteByIds', util.toFormData({
                            ids: this.ids
                        })).then(res => {
                            if (res.data.message === 'SUCCESS') {
                                this.$Message.success('删除成功！');
                                this.loadList();
                            }
                        });
                    }
                });
            } else {
                this.$Modal.error({
                    content: '请选择要删除用户！'
                });
            }
        },
        addMember () {
            this.formData = {};
            this.editModal = true;
            this.loadDip();
        },
        saveMember (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let formData = {};
                    for (var key in this.formData) {
                        formData[key] = this.formData[key];
                    }
                    formData.themeId = this.theme;
                    formData.orgid = this.baseuser.orgId;
                    util.ajax.post('/busine/theme/member/saveMember', util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('会员添加成功！');
                            this.editModal = false;
                            this.loadList();
                        }
                    });
                }
            });
        },
        closeModel (formName) {
            this.$refs[formName].resetFields();
            this.editModal = false;
        }
    }
};
